package com.yang.controller;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yang.entity.User;
import com.yang.service.UserService;

/**
 * 描述：登录相关的控制器
 * 版本：1.0 
 * 作者： 杨建全
 * 创建时间：2016年3月23日  上午10:02:50
 */
@Controller
public class LoginController{
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "/login";
	}
	
	@RequestMapping(value="/loginSubmit",method=RequestMethod.POST)
	public String loginSubmit(User user){
		Subject userSubject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
	//	token.setRememberMe(true);
        try {
        	userSubject.login(token);
        	
            return "redirect:/adminIndex";
        }catch (AuthenticationException e) {
        	System.out.println("登录失败！");
            token.clear();
            return "/login";
        }
	}
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		SecurityUtils.getSubject().logout();
		return "/login";
	}
	
	
}
